﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using TShooter.TfsWord.Resources;
using TShooter.TfsWord.TeamFoundation;

namespace TShooter.TfsWord.Win.UI
{
    public partial class DocumentCreationDialog : Form
    {
        #region Constructors and member variables

        private readonly ConnectionInfo _ConnectionInfo;
        private ITfsConnection _Connection;
        private List<int> _WorkItemIds;

        public DocumentCreationDialog(ConnectionInfo connectionInfo)
        {
            _ConnectionInfo = connectionInfo;
            InitializeComponent();
        }

        #endregion

        #region Properties

        public ITfsConnection Connection
        {
            get { return _Connection ?? (_Connection = ConnectionFactory.GetConnection(_ConnectionInfo)); }
        }

        public DocumentCreationDialogResult Result { get; private set; }

        #endregion

        public new DocumentCreationDialogResult ShowDialog()
        {
            base.ShowDialog();

            return Result;
        }

        private void comboBox1_DropDown(object sender, EventArgs e)
        {
            if (cmbWorkItemType.Items.Count != 0) return;

            using (Hourglass.Show())
            {
                foreach (string project in Connection.GetWorkItemTypeDefinitions())
                {
                    cmbWorkItemType.Items.Add(project);
                }
            }
        }

        private void cmbWorkItemType_Validating(object sender, CancelEventArgs e)
        {
            ValidateDialogSelection();
        }

        private void ValidateDialogSelection()
        {
            UpdateWorkItemSelectionStatus();

            btnOk.Enabled = ((optNewDocument.Checked && cmbWorkItemType.SelectedIndex > -1) ||
                             (optOpenDocument.Checked && _WorkItemIds != null));
        }

        private void lnkSelectWorkItem_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            PickWorkItems();
        }


        public IEnumerable<int> GetSelectedWorkItems()
        {
            return _WorkItemIds;
        }

        private void PickWorkItems()
        {
            IEnumerable<int> selectedWorkItems = null;

            using (var picker = new WorkItemPickerDialog())
            {
                DialogResult result = picker.ShowDialog(_ConnectionInfo.TeamProjectCollectionUri,
                                                        _ConnectionInfo.TeamProject);

                if (result == DialogResult.OK)
                {
                    selectedWorkItems = picker.GetSelectedWorkItems();
                }
            }

            if (selectedWorkItems == null)
            {
                _WorkItemIds = null;
                ValidateDialogSelection();
                return;
            }

            _WorkItemIds = new List<int>(selectedWorkItems);
            optOpenDocument.Checked = true;

            ValidateDialogSelection();
        }

        private void UpdateWorkItemSelectionStatus()
        {
            txtWorkItemSelectionStatus.Text = (_WorkItemIds == null
                                                   ? Strings.DocumentCreationDialog_NoWorkItemsSelected
                                                   : string.Format(Strings.DocumentCreationDialog_WorkItemsSelected,
                                                                   _WorkItemIds.Count));
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            Result = new DocumentCreationDialogResult
                         {
                             DialogResult = DialogResult.Cancel,
                             SelectedWorkItems = GetSelectedWorkItems(),
                             WorkItemTypeDefinition = cmbWorkItemType.Text,
                             CreationMode =
                                 (optNewDocument.Checked
                                      ? DocumentCreationMode.FromScratch
                                      : DocumentCreationMode.FromExistingWorkItems)
                         };

            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Result = new DocumentCreationDialogResult
                         {
                             DialogResult = DialogResult.Cancel
                         };

            Close();
        }

        #region Nested type: DocumentCreationDialogResult

        public class DocumentCreationDialogResult
        {
            public DialogResult DialogResult { get; internal set; }
            public IEnumerable<int> SelectedWorkItems { get; internal set; }
            public string WorkItemTypeDefinition { get; internal set; }
            public DocumentCreationMode CreationMode { get; internal set; }
        }

        #endregion
    }
}